package com.itheima.reggie.controller;

import com.itheima.reggie.common.ResultInfo;
import com.itheima.reggie.domain.Category;
import com.itheima.reggie.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

//分类模块
@RestController  //控制器类
public class CategoryController {

    //需要调用service(业务层)
    @Autowired
    private CategoryService categoryService;

    //分类列表
    @GetMapping("/category/findAll")
    public ResultInfo findAll(){ //接收请求
        //2.调用service查询
       List<Category> categoryList =  categoryService.findAll();
        //3.返回结果
        return ResultInfo.success(categoryList);
    }
    //新增分类
    @PostMapping("/category")
    public ResultInfo save(@RequestBody Category category){ //接收post请求参数
        //2.调用service添加
        categoryService.save(category);
        //3.返回成功
        return ResultInfo.success();
    }
    //修改分类
    @PutMapping("/category")
    public ResultInfo findById(@RequestBody Category category){ //接收put请求参数
        //2.调用service修改
        categoryService.updateById(category);
        //返回成功
        return ResultInfo.success();
    }
    //删除分类
    @DeleteMapping("/category")
    public ResultInfo deleteById(Long id){ //接收参数
        //调用service删除
       //ResultInfo resultInfo =  categoryService.deleteById(id);
        categoryService.deleteById(id);
       //返回结果
        return ResultInfo.success();
    }
    //分类回显
    @GetMapping("/category/list")
    public ResultInfo findByType(Integer type){ //1.接收参数
        //2.根据类型查询分类列表
       List<Category> categoryList =  categoryService.findByType(type);
        //3.返回结果
        return ResultInfo.success(categoryList);
    }
}
